
# generate table 3

library(rio) # to load data
library(tidyverse) # to reshape data
library(AER) # to estimate tobits
library(lmtest) # to cluster standard errors
library(sandwich) # to cluster standard errors
library(texreg) # to generate tables

# set your working directory
setwd("~/replication_files")

monthly_data <- import("data/monthly_data.csv")

# NOC
data_models_noc <- monthly_data %>%
  filter(iso3c == "ARG" | iso3c == "BOL" | iso3c == "BRA" | iso3c == "CHL" | iso3c == "COL" |
           iso3c == "ECU" | iso3c == "MEX" | iso3c == "PAR" | iso3c == "PER" | iso3c == "SUR" | 
           iso3c == "URY" | iso3c == "TTO" | iso3c == "VEN")

# table 3

# model 1
probit_noc <- glm(noc_issued_1yr ~
                    resource_rents_perc_lag +
                    log_oil_production_lag +
                    terms_of_trade_lag +
                    discovery_lag +
                    minister_is_econ_technocrat_usa +
                    minister_turnover_5yrs +
                    debt_crisis_experience +
                    election_month + left_exec +
                    fiscal_council + 
                    polcon + 
                    imf_agreement +
                    fiscal_balance_lag + 
                    tax_perc_gdp_cepal_lag +
                    log_core_inflation_lag +
                    gdp_pc_cepal_lag +
                    gdp_growth_cepal_lag +
                    ka_open_lag +
                    log_reserves_lag +
                    treasury_rate_lag +
                    I(iso3c) +
                    time,
                  data = data_models_noc,
                  family = binomial(link = "probit"))

probit_noc_r <- probit_noc %>%
  coeftest(vcovHC(probit_noc, type = 'HC0', cluster = c('iso3c','Summary')))

# model 2
tobit_noc <- tobit(log_noc_issued_1yr_amount ~
                     resource_rents_perc_lag +
                     log_oil_production_lag +
                     terms_of_trade_lag +
                     discovery_lag +
                     minister_is_econ_technocrat_usa +
                     minister_turnover_5yrs +
                     debt_crisis_experience +
                     election_month + left_exec +
                     fiscal_council + 
                     polcon + 
                     imf_agreement +
                     fiscal_balance_lag + 
                     tax_perc_gdp_cepal_lag +
                     log_core_inflation_lag +
                     gdp_pc_cepal_lag +
                     gdp_growth_cepal_lag +
                     ka_open_lag +
                     log_reserves_lag +
                     treasury_rate_lag +
                     I(iso3c) +
                     time,
                   data = data_models_noc, robust = T)

texreg(l = list(probit_noc_r,tobit_noc), stars = c(0.01, 0.05, 0.1),
       custom.header = list("Dependent Variable:" = 1:2),
       custom.model.names = c("NOC Issued (Yes = 1)",
                              "Ln NOC Amount Issued"),
       custom.coef.map = list("resource_rents_perc_lag" = "Resource Rents, % of GDP $_{t-1}$",
                              "log_oil_production_lag" = "Ln Oil and Gas Production $_{t-1}$",
                              "terms_of_trade_lag" = "Commodity Price Index $_{t-1}$",
                              "discovery_lag" = "Field Discovery $_{t-1}$",
                              "minister_is_econ_technocrat_usa" = "Mainstream Minister = 1",
                              "minister_turnover_5yrs" = "Minister Turnover (5 Years)",
                              "debt_crisis_experience" = "Debt Crisis Experience = 1",
                              "election_month" = "Election Month = 1",
                              "left_exec" = "Left Executive = 1",
                              "fiscal_council" = "Fiscal Council = 1",
                              "polcon" = "Political Constraints (POLCON)",
                              "imf_agreement" = "IMF Agreement = 1",
                              "fiscal_balance_lag" = "Fiscal Balance, % of GDP $_{t-1}$",
                              "tax_perc_gdp_cepal_lag" = "Tax Revenue, % of GDP $_{t-1}$",
                              "log_core_inflation_lag" = "Ln Core Inflation $_{t-1}$",
                              "gdp_pc_cepal_lag" = "GDP Per Capita $_{t-1}$",
                              "gdp_growth_cepal_lag" = "GDP Growth, % $_{t-1}$",
                              "ka_open_lag" = "Capital Openness $_{t-1}$",
                              "log_reserves_lag" = "Ln International Reserves $_{t-1}$",
                              "treasury_rate_lag" = "US Treasury Rate, % $_{t-1}$",
                              "Log(scale)" = "Log(Scale)"),
       fontsize = "footnotesize", digits = 3)

# retrieve fit stats
texreg(l = list(probit_noc,tobit_noc), stars = c(0.01, 0.05, 0.1))